<template>
  <div>
    <slot></slot>
  </div>
</template>

<script>
  import { findComponentsDownward } from '../../utils/assist'
  import Emitter from '../../utils/emitter'
  export default {
    name: 'iCheckboxGroup',
    mixins: [Emitter],
    components: {},
    props: {
      value: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        currentVal: this.value,
        children: []
      }
    },
    watch: {
      value() {
        this.updateModel(true)
      }
    },
    mounted() {
      this.updateModel(true)
    },
    methods: {
      updateModel(update) {
        // 获取所有的checkbox组件
        this.children = findComponentsDownward(this, 'iCheckbox')
        if (this.children.length) {
          const { value } = this
          this.children.forEach(child => {
            child.model = value

            if (update) {
              // 如果model的当前值在数组中，则currentValue为true
              child.currentValue = value.indexOf(child.label) >= 0
              child.group = true
            }
          })
        }
      },
      change (data) {
        this.currentValue = data
        this.$emit('input', data)
        this.$emit('on-change', data)
        this.dispatch('iFormItem', 'on-form-change', data)
      }
    }
  }
</script>
<style lang='scss' scoped>
</style>
